package com.nuoniu.sibanyun.controller.cms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.cms.CmsArticle;
import com.nuoniu.sibanyun.service.cms.ICmsArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 文章管理 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/cms/article")
@Api(tags = "文章管理")
public class CmsArticleController extends BaseController {

    @Autowired
    private ICmsArticleService articleService;

    @ApiOperation("获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, CmsArticle article) {
        return HttpResult.ok(articleService.pageList(page, article));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody CmsArticle article) {
        if(article == null || StringUtils.isBlank(article.getTitle())) {
            return HttpResult.error("标题不能为空");
        }

        if(StringUtils.isBlank(article.getContent())) {
           return HttpResult.error("内容不能为空");
        }

        articleService.saveOrUpdate(article);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        articleService.removeById(id);
        return HttpResult.ok();
    }

}
